<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/js/jquery.json-2.4.js"></script>
<script>

$(function(){
							$.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });
							$("#date_from").datepicker({  
							    onSelect:function(dateText,inst){  
							       $("#date_to").datepicker("option","minDate",dateText);  
							    }  
							});  
							$("#date_to").datepicker({  
							    onSelect:function(dateText,inst){  
							        $("#date_from").datepicker("option","maxDate",dateText);  
							    }  
							});
	$("#date_from").val('<?php echo isset($date_from)?$date_from:""; ?>');
	$("#date_to").val('<?php echo isset($date_to)?$date_to:""; ?>');
	$("#purchaseorder_id").val('<?php echo isset($purchaseorder_id)?$purchaseorder_id:""; ?>');
	$("#purchase_id").val('<?php echo isset($purchase_id)?$purchase_id:""; ?>');
	$("#supplier_name").val('<?php echo isset($supplier_name)?$supplier_name:""; ?>');
	$("#pay_status").val('<?php echo isset($pay_status)?$pay_status:"10"; ?>');
	//初始化checkbox的状态
	$("input[type='checkbox']").each(function(){
		if(this.checked){
			this.checked=false;
		}
	});
	var ids="-";
	//点击全选和全不选
	$("#chooseall").click(function(){
		if(this.checked){ 
			$("input[name='choose']").each(function(){
				this.checked=true;
						
				//如果该复选框已经被选择，则获取其采购单的ID
				if(this.checked){
					var id=$(this).val();
					if(ids.indexOf(id)==-1){
							ids=ids+id+"-";
					}
				}
			});
		}else{ 
				ids="-";
				$("input[name='choose']").each(function(){
					this.checked=false;
				}); 
		}
	});
		//点击单个单选按钮将其id拼入或者删除
		$("input[name='choose']").click(function(){
			var id=$(this).val();
			if(this.checked){
				if(ids.indexOf(id)==-1){
					ids=ids+id+"-";
				}
				
			}else{
				var index=ids.indexOf(id);
				if(index!=-1){
					var idremove=ids.substr(index,id.length);
					var reg="-"+idremove+"-";
					ids=ids.replace(new RegExp(reg),"-");
				}
			}
		});
		//点击打印按钮，调用后台php代码
		$('#tijiao').click(function(){

			if(ids=='-'){
				alert("请选择采购付款单");
				return;
			}else{

				var strs= new Array();
				strs=ids.split("-");
				var arr = new Array();
				for(var i=1;i<(strs.length-1);i++)
				{
						var flag = valicate_data($("#pay_"+strs[i]).val(),$("#paid_"+strs[i]).text(),$("#money_"+strs[i]).text(),$("#remark_"+strs[i]).val());
						if(!flag)
							return ;
						arr[i] = {'detail' : $("#detail_"+strs[i]).text(), 'sku': $("#sku_"+strs[i]).text(), 
											'money' : $("#money_"+strs[i]).text(),   'paid': $("#paid_"+strs[i]).text(),  
						    			'pay': $("#pay_"+strs[i]).val(),'remark':$("#remark_"+strs[i]).val()};
				}
				var jdata=$.toJSON(arr);
				$.ajax({
					type:'POST',
					dataType:"json",
					url:"<?php echo URL::site() ?>purchases/pay/batch_pay",
					async:false,
					data:{json:jdata},
					success:function(result){					
					alert("付款成功");
					window.location.reload();
				},		
				});
			}
		}); 

		$(".payback").click(function(){
			var pre = "payback_";
			var purchaseorder_detail_id = $(this).attr('id').substr(pre.length);
			var url = "<?php echo url::base(true)?>purchases/pay/purchase_payback?purchase_detail_id=";
			url += purchaseorder_detail_id;
			
			//alert(url);
			window.open(url);
		});
				
		 
});
	
function paymoney(id,purchaseorder_id)
{
		var factpay = $("#factpay_"+id).val();
		var remark  = $("#remark_"+id).val();
		var paid    = $("#paid_"+id).text();
		var ownpay  = $("#ownpay_"+id).text();
		tijiao(id,purchaseorder_id,factpay,remark,paid,ownpay);
}
function valicate_data(factpay,paid,ownpay,remark)
{
	var flag    = judge(factpay);
	if(!flag||factpay<= 0)
		{
			if(!flag)
				alert("金额非数字");
			else			
				alert("实付金额小于等于0");
			return false;
		}
		
		if(parseFloat(paid)>=parseFloat(ownpay))
		{
			alert("已完全付款");return false;
		}
		var mark = parseFloat(factpay)+parseFloat(paid)-parseFloat(ownpay);
		
		if( mark>0)	
					{
						var result=window.confirm("实际付款大于应付金额,确认要付款吗？");						
						if(result)
						{
	      			if(remark=="")
							{
									alert("备注为空");return false;
							}
						}else{
							
							return false;
						}
					}
	return true;
}
function tijiao(id,purchaseorder_id,factpay,remark,paid,ownpay)
{

		var flag    = judge(factpay);
		if(!flag||factpay<= 0)
		{
			return ;
		}
		if(paid>=ownpay)
		{
			alert("已完全付款");return false;
		}
		var mark = parseFloat(factpay)+parseFloat(paid)-parseFloat(ownpay);
		if( mark>0)	
					{
						var result=window.confirm("实际付款大于应付金额,确认要付款吗？");						
						if(result)
						{
	      			if(remark=="")
							{
									alert("备注为空");return ;
							}
						}else{
							
							return ;
						}
					}
			 $.ajax({
       type:'GET',
       url:"<?php echo URL::base() ?>purchases/pay/submit_pay",
       async:false,
       data:{factpay:factpay,remark:remark,id:id,purchaseorder_id:purchaseorder_id,paid:paid,ownpay:ownpay},
       success:function(result){
					if(result){
						
					}
					else{
												
					}
			 },
       error:function(result){
				alert("error");
			 },
       dataType:"json"
       });
}
function judge(factpay){

	if(!isNaN(factpay)){
		return true;;
	}else{
		alert("不是数字"); 
		return false;
	}
}

function commit()
{	

}


</script>

<style>

.lab1{ width:90px; text-align:right; display:inline-block;  margin-bottom:10px; margin-right:3px;}
</style>
<html>
<body>
<form action="<?php echo URL::base()?>purchases/pay/purchasepaySave" method="get" name="form1">
													<div>
														<span class="lab1">单据日期:  </span>
                               
                               
                                  <input style=" margin-bottom:10px;" type="text" name="date_from" id="date_from" class="datepicker" value="">
		<span class="sk1"> ~ </span><input style=" margin-bottom:10px;" type="text" name="date_to" id="date_to" class="datepicker" value="">
                               
                               
													</div>

													<div id="pid">
														<span class="lab1">采购单号:  </span>
									
																<input style=" margin-bottom:10px;" type="text" name="purchaseorder_id" id="purchaseorder_id">
														
													</div>
													<div>
														<!--<label class="control-label" for="input01">SKU</label>
                                <div class="controls">
                                  <input type="text" name="sku" class="sku">
                                </div>
													</div>-->
													<div>
														<span class="lab1">供应商:  </span>
                              
                                  <input style=" margin-bottom:10px;" type="text" name="supplier_name" id="supplier_name">
                              
													</div>
                          <div>
                                <span class="lab1">付款状态:  </span>
                               
                                  <select style=" margin-bottom:10px;width: 219.5px" class="input-xlarge"  name="pay_status" id="pay_status" >
                                		<option value="10">未付款</option>
                                   	<option value="20">部分已付款</option>
                                    <option value="30">全部付款</option>
                                    <option value="40">完结采购</option>
                                  </select>
                            
                              </div>


<div id="purchaser">
<span class="lab1">采购人员:  </span><input style=" margin-left:5px;" type="text" name="purchase_id" id="purchase_id"><div></div>

	<div style=" margin-left:100px; margin-top:10px;" ><input type="submit" id="select_button" value="查询" class="btn btn-info"></div>
</div>
</form>
<br/>
<div style=" margin-left:10px;" id="result_purchase">
				<?php foreach($result as $_result): ?>
																								<table style=" margin-bottom:20px; border:1px solid #ddd;" class="table table-bordered" id="table_<?php echo $_result['id'] ?>">
																											<tbody>
																												<tr style=" background-color: rgba(129,208,177,.3);">
																												<td style=" font-weight:bold;">采购单ID</td>
																												<td >应付金额</td>
																												<td colspan="3">已付金额</td>
																												<td colspan="3">供应商</td>
																												</tr>
																												<tr>
																											<!-- 供应商信息 -->
																												<td><?php echo $_result['purchaseorder_id'];?></td>
																												<td ><?php echo Purchase::select_total_purchase($_result['purchaseorder_id'])?></td>
																												<td colspan="3"><?php echo Purchase::select_totals_purchase($_result['purchaseorder_id'])?></td>
																												<td colspan="3"><?php echo Purchase::select_s_name($_result['supplier_id'])?Purchase::select_s_name($_result['supplier_id']):"无供应商";?></td>
																												</tr>
																											<!-- end 供应商信息 -->
																												<tr style=" background-color: rgba(129,208,177,.3);">
																												<td>选择</td>
																												<td>采购单明细ID</td>
																												<td>SKU</td>
																												<td>应付金额</td>
																												<td>已付金额</td>
																												<td>本次应付金额</td>
																												<td>备注</td>
																												<td>操作</td>
																												</tr>
																												<?php $purchase_detail = Purchase::select_purchase_detail($_result['purchaseorder_id'])?>
																												<?php if(isset($purchase_detail)&&!empty($purchase_detail)){?>
																												<?php foreach($purchase_detail as $value){?>
																												<tr>
																												<td>
																												<?php 
																													$paid = Purchase::select_paid($value['id']);
																												  if($value['money']>$paid){?>
																												<input type="checkbox" name="choose" value="<?php echo $value['id']?>">																								<?php }?>
																												</td>
																												<td id = "detail_<?php echo $value['id']?>"><?php echo $value['id']?></td>
																												<td id = "sku_<?php echo $value['id']?>"><?php echo $value['sku']?></td>
																												<td id = "money_<?php echo $value['id']?>"><?php echo $value['money']?></td>
																												<td id = "paid_<?php echo $value['id']?>"><?php echo Purchase::select_paid($value['id']);?></td>
																												<td><input type="text" id = "pay_<?php echo $value['id']?>" value="<?php echo $value['money']- Purchase::select_paid($value['id']);?>"></td>
																												<td><input type="text" id = "remark_<?php echo $value['id']?>" ></td>
																												<td><input  type="button" id = "payback_<?php echo $value['id']?>"
																												class="btn btn-info payback" value="供应商退款"></td>
																												</tr>
																												<?php }?>
																												<?php }?>
																												</tbody>
																								</table> 


		</table>		<?php endforeach;?>

<?php if(isset($pagination)) echo $pagination;?> <input style=" margin:0;" type="checkbox"  id="chooseall">&nbsp;
全选 
<br/>
<br/>
<input type="button" value="一键提交" class='btn btn-info' onclick="" id="tijiao">
<input type="text" name="ids" id="ids" value="" style="display:none"/>
</body>
</html>
